<?php
require APPPATH.'libraries/ApiController.php';
class payment extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	//购买产品订单
	public function pay_product(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"],
			["username",true,"用户名"],
			["paytype",true,"支付方式"],
			["finalsum",true,"订单总额"],
			["buylist",true,"订单商品"],
			],'post');
			
			$data["sumrmb"]=$data["finalsum"];
			unset($data["finalsum"]);
			$paytype=json_decode($data["paytype"],true);
			$data["ordercode"]='PG'.date("Ymd").mt_rand(100000, 999999);
			$data["shopid"]=$token["shopid"];
			
			//处理afterrmb
			if($data["uid"]!=-1){
				$user=$this->db->select("uid,username,money,score,times")->where("uid",$data["uid"])->where("shopid",$data["shopid"])->get("user")->row_array();
				//余额支付
				if((int)$paytype["ptid"]==2){ 
					if((float)$user["money"]<(float)$data["sumrmb"]){
						$this->WRITE("code",0);
						$this->WRITE("msg","会员余额不足,请先充值!");	
						$this->RESPONSE();
					}else{
						$data["afterrmb"]=$user["money"]-(float)$data["sumrmb"];
						$data["paytypeid"]=$paytype["ptid"];
						$data["paytypetitle"]=$paytype["pttitle"];
						$this->db->insert("payment_rmb",$data);
						
						$this->db->where("uid",$data["uid"]);
						$this->db->where("shopid",$data["shopid"]);
						$this->db->update("user",array("money"=>$data["afterrmb"]));
						
						$data["paytype"]=$paytype;
						$data["createtime"]=date("Y-m-d H:i:s");
						$this->WRITE("code",1);
						$this->WRITE("data",$data);
						$this->WRITE("msg","付款成功！");
						$this->RESPONSE();
					}				
				}else{
					$data["afterrmb"]=$user["money"];
					$data["paytypeid"]=$paytype["ptid"];
				
					$data["paytypetitle"]=$paytype["pttitle"];
					$this->db->insert("payment_rmb",$data);
					
					$this->db->where("uid",$data["uid"]);
					$this->db->where("shopid",$data["shopid"]);
					$this->db->update("user",array("money"=>$data["afterrmb"]));
					
					$data["paytype"]=$paytype;
					$data["createtime"]=date("Y-m-d H:i:s");
					$this->WRITE("code",1);
					$this->WRITE("data",$data);
					$this->WRITE("msg","付款成功！");
					$this->RESPONSE();
				}			
			}else{
				$data["afterrmb"]=0;
				$data["paytypeid"]=$paytype["ptid"];
				
				$data["paytypetitle"]=$paytype["pttitle"];
				$this->db->insert("payment_rmb",$data);
				
				$data["paytype"]=$paytype;
				$data["createtime"]=date("Y-m-d H:i:s");
				$this->WRITE("code",1);
				$this->WRITE("data",$data);
				$this->WRITE("msg","付款成功！");
				$this->RESPONSE();
			}

		}
	}
	
	/*新增消卡记录*/
	public function pay_timecard(){
		$token=$this->verifyToken();
		if($token){
		$data=$this->getKeys([
			["uid",true,"会员ID"],
			["tcrid",true,"次卡记录ID"],
			["username",true,"会员姓名"],
			["carddata",true,"消次卡数据"],
			["paycount",true,"消卡次数"],
			["memo",false,"消次备注",""],
			],'post');
			
			$carddata=$this->db->select("sumrmb,chargecount,count")->where("tcrid",$data["tcrid"])->get("charge_timecard")->row_array();
			$card=json_decode($data["carddata"],true);
			if((int)$data["paycount"]<=($carddata["count"])){
				$aftertimes=($carddata["count"]-$data["paycount"]);
				$rdata=array(
					"ordercode"=>'PT'.date("Ymd").mt_rand(10000, 99999),
					"uid"=>$data["uid"],
					"tcrid"=>$card["tcrid"],
					"cardtitle"=>$card["timecardtitle"],
					"count"=>$data["paycount"],
					"rmb"=>((float)$carddata["sumrmb"]/$carddata["chargecount"])*$data["paycount"],
					"aftertimes"=>$aftertimes,
					"timecarddata"=>$data["carddata"],
					"shopid"=>$token["shopid"],
					"memo"=>$data["memo"]);
				//创建消费记录
				$this->db->insert("payment_timecard",$rdata);
				
				//同步充值记录上的剩余次数
				$this->db->where("tcrid",$data["tcrid"]);
				$this->db->update("charge_timecard",array("count"=>$aftertimes));			
				
				
				//计算所有可用次数
				//计算长期有效期内的卡
				$this->db->where("shopid",$token["shopid"]);
				$this->db->where("uid",$data["uid"]);
				$this->db->select("tcrid,ordercode,uid,username,shopid,timecardtitle,expdate,chargecount,count,memo,createtime");
				$cardlist=$this->db->get("charge_timecard")->result_array();
				
				$curdate=strtotime(date('Y-m-d 00:00:00'));
				$sumcount=0;
				foreach($cardlist as $card){
					if($card["expdate"]===""){
						$sumcount+=(int)$card["count"];
					}else if($card["expdate"]!=="")
					{
						$expdate=strtotime($card["expdate"].' 23:59:59');
						if($expdate>$curdate){
							$sumcount+=(int)$card["count"];
						}else{
							
						}
					}
				}
				
				//统计支付
				$this->db->where("uid",$data["uid"]);
				$this->db->update("user",array("times"=>$sumcount));
				

				//构建订单数据
				$orderdata=array(
				  "ordercode"=>$rdata["ordercode"],
				  "uid"=>$rdata["uid"],
				  "username"=>$data["username"],
				  "cardtitle"=>$rdata["cardtitle"],
				  "count"=>$rdata["count"],
				  "aftertimes"=>$aftertimes,
				  "createtime"=>date("Y-m-d H:i:s")
				);
				
				$this->WRITE("code",1);
				$this->WRITE("result",$orderdata);
				$this->WRITE("msg","消次成功");
				$this->RESPONSE();	
				
				
			}else{
				$this->WRITE("code",0);
				$this->WRITE("msg","消费次数不足!");
				$this->RESPONSE();	
			}
		}
	}
	
	public function pay_quick(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"],
			["username",true,"用户名"],
			["paytype",true,"支付方式"],
			["sumrmb",true,"订单总额"],
			["buylist",true,"订单商品"],
			],'post');
			
			$paytype=json_decode($data["paytype"],true);
			$data["ordercode"]='PQ'.date("Ymd").mt_rand(100000, 999999);
			$data["shopid"]=$token["shopid"];
			
			//处理afterrmb
			if($data["uid"]!=-1){
				$user=$this->db->select("uid,username,money,score,times")->where("uid",$data["uid"])->where("shopid",$data["shopid"])->get("user")->row_array();
				//余额支付
				if((int)$paytype["ptid"]==2){ 
					if((float)$user["money"]<(float)$data["sumrmb"]){
						$this->WRITE("code",0);
						$this->WRITE("msg","会员余额不足,请先充值!");	
						$this->RESPONSE();
					}else{
						$data["afterrmb"]=$user["money"]-(float)$data["sumrmb"];
						$data["paytypeid"]=$paytype["ptid"];
						$data["paytypetitle"]=$paytype["pttitle"];
						$this->db->insert("payment_rmb",$data);
						
						$this->db->where("uid",$data["uid"]);
						$this->db->where("shopid",$data["shopid"]);
						$this->db->update("user",array("money"=>$data["afterrmb"]));
						
						$data["paytype"]=$paytype;
						$data["createtime"]=date("Y-m-d H:i:s");
						$this->WRITE("code",1);
						$this->WRITE("data",$data);
						$this->WRITE("msg","收费成功！");
						$this->RESPONSE();
					}				
				}else{
					$data["afterrmb"]=$user["money"];
					$data["paytypeid"]=$paytype["ptid"];
				
					$data["paytypetitle"]=$paytype["pttitle"];
					$this->db->insert("payment_rmb",$data);
					
					$this->db->where("uid",$data["uid"]);
					$this->db->where("shopid",$data["shopid"]);
					$this->db->update("user",array("money"=>$data["afterrmb"]));
					
					$data["paytype"]=$paytype;
					$data["createtime"]=date("Y-m-d H:i:s");
					$this->WRITE("code",1);
					$this->WRITE("data",$data);
					$this->WRITE("msg","收费成功！");
					$this->RESPONSE();
				}			
			}else{
				$data["afterrmb"]=0;
				$data["paytypeid"]=$paytype["ptid"];
				
				$data["paytypetitle"]=$paytype["pttitle"];
				$this->db->insert("payment_rmb",$data);
				
				$data["paytype"]=$paytype;
				$data["createtime"]=date("Y-m-d H:i:s");
				$this->WRITE("code",1);
				$this->WRITE("data",$data);
				$this->WRITE("msg","收费成功！");
				$this->RESPONSE();
			}
			
		}
	}
	

	
	
	public function history_rmb (){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$this->db->where("shopid",$token["shopid"]);
			$this->db->order_by("createtime",'desc');
			$list=$this->db->get("payment_rmb")->result_array();
			
			for($i=0;$i<count($list);$i++){
				$list[$i]["paytype"]=json_decode($list[$i]["paytype"],true);
				$list[$i]["buylist"]=json_decode($list[$i]["buylist"],true);
			}
			
			$this->WRITE("code",1);
			$this->WRITE("msg","查询成功!");	
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
	
	//几次消费
	public function history_timecard(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"会员ID"]],'post');
			
			$this->db->where("uid",$data["uid"]);
			$this->db->where("shopid",$token["shopid"]);
			$this->db->order_by("createtime",'desc');
			$list=$this->db->get("payment_timecard")->result_array();
			
			$this->WRITE("code",1);
			$this->WRITE("msg","查询成功!");	
			$this->WRITE("list",$list);
			$this->RESPONSE();
		}
	}
}